﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchoolApplication.Models;

namespace GameSchoolApplication.Controllers
{ 
	[Authorize]
    public class AnnouncementController : Controller
    {
        private GameSchoolWebContext _db = new GameSchoolWebContext();

        // GET: /Announcement/
        public ViewResult Index()
        {
            var announcements = _db.Announcements.Include(a => a.Game).OrderByDescending(a => a.DateTime);
            return View(announcements.ToList());
        }


		/// <summary>
		/// Gets all the students announcements from his games
		/// </summary>
		/// <returns></returns>
        public ActionResult StudentIndex()
        {
            Person student = _db.Persons.Where(p => p.UserName == User.Identity.Name).SingleOrDefault();
            var announcements = student.AllAnnouncements;
            return View(announcements);
        }

        // GET: /Announcement/Details/5
        public ViewResult Details(int id)
        {
            Announcement announcement = _db.Announcements.Find(id);
            return View(announcement);
        }

        // GET: /Announcement/Create
        [HttpGet]
        public ActionResult Create()
        {
            ViewBag.GameId = new SelectList(_db.Games, "GameId", "Title");
            return View();
        } 

        // POST: /Announcement/Create
        [HttpPost]
        public ActionResult Create(Announcement announcement)
        {
            announcement.DateTime = DateTime.Now;
            if (ModelState.IsValid)
            {
                _db.Announcements.Add(announcement);
                _db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.GameId = new SelectList(_db.Games, "GameId", "Title", announcement.GameId);
            return View(announcement);
        }
        
        // GET: /Announcement/Edit/5
        [HttpGet]
        public ActionResult Edit(int id)
        {
            Announcement announcement = _db.Announcements.Find(id);
            ViewBag.GameId = new SelectList(_db.Games, "GameId", "Title", announcement.GameId);
            return View(announcement);
        }

        // POST: /Announcement/Edit/5
        [HttpPost]
        public ActionResult Edit(Announcement announcement)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(announcement).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.GameId = new SelectList(_db.Games, "GameId", "Title", announcement.GameId);
            return View(announcement);
        }

        // GET: /Announcement/Delete/5 
        public ActionResult Delete(int id)
        {
            Announcement announcement = _db.Announcements.Find(id);
            return View(announcement);
        }

        // POST: /Announcement/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Announcement announcement = _db.Announcements.Find(id);
            _db.Announcements.Remove(announcement);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}